package com.medical.medical.service;

import com.medical.medical.entity.Patient;
import com.medical.medical.entity.Record;
import com.medical.medical.mapper.PatientMapper;
import com.medical.medical.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 * @authorDesc 你相信光吗？
 * @author GUOYN
 * @date 2022-09-14 09:47:08
 * @version 1.0.0
 * @description 用户/患者业务逻辑层实现类
 */
@Slf4j
@Service
public class PatientServiceImpl implements PatientService{
    @Resource
    private PatientMapper patientMapper;
    /**
     * @Description: 患者用户验证登录
     * @author: 喜杨杨
     * @date: 2022/9/21 17:01
     * @param: [phone, password]
     * @return: com.medical.medical.entity.Patient
     **/
    public Patient login(String phone, String password) {

        Patient patient = null;
        try {
            patient = patientMapper.getLoginUser(phone);
            log.info("查出来的密码"+patient.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //匹配密码
        if(null != patient){

            String pwd= MD5Util.convertMD5(patient.getPassword());
            log.info("解密后密码"+pwd);
            if(!pwd.equals(password)) {
                patient = null;
            }
        }
        return patient;
    }

    /**
     * @Description: 患者用户注册
     * @author: 喜杨杨
     * @date: 2022/9/21 17:09
     * @param: [patient]
     * @return: boolean
     **/
    @Override
    public boolean addPatient(Patient patient) {
        boolean flag = false;
        log.info("添加");
        try {
            int updateRows = patientMapper.addPatient(patient);

            if(updateRows > 0){
                flag = true;
                log.info("addPatient success!");
            }else{
                log.info("addPatient failed!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加失败!!!");
        }
        return flag;
    }
    
    /**
      * @description 获得诊断报告
      * @author ChenLi
      * @date 2022-09-26 15:43:14
      * @param id
      * @return {@link List< Record>}
      */
    @Override
    public List<Record> getReport(int id) {
        List<Record> recordList = patientMapper.getReport(id);
        return recordList;
    }
    /**
     * @description 获得条件下表中数据总条数
     * @author GUOYN
     * @date 2022-09-14 10:30:41  * @param phone
     * @param name
     * @param idCard
     * @return {@link int}
     */
    @Override
    public int getPatientCount(long phone, String name, String idCard) {
        try {
            return patientMapper.getPatientCount(phone, name, idCard);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获得条件下表中数据总条数时发生异常！");
        }
        return 0;
    }
    /**
     * @description 获得当前页面信息
     * @author GUOYN
     * @date 2022-09-14 10:34:50  * @param phone
     * @param name
     * @param idCard
     * @param start
     * @param pageSize
     * @return {@link List< Patient>}
     */
    @Override
    public List<Patient> getPatientList(long phone, String name, String idCard, int start, int pageSize) {
        try {
            return patientMapper.getPatientList(phone, name, idCard, start, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获得当前页面信息时发生异常！");
        }
        return null;
    }
    /**
     * @description 根据id获得当前用户详细信息
     * @author GUOYN
     * @date 2022-09-14 11:56:53  * @param id
     * @return {@link Patient}
     */
    @Override
    public Patient getPatientView(int id) {
        try {
            return patientMapper.getPatientView(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据id获得当前用户详细信息时发生异常！");
        }
        return null;
    }
    /**
      * @description 根据id删除用户/患者
      * @author GUOYN
      * @date 2022-09-14 12:06:50  * @param id
      * @return {@link boolean}
      */
    @Override
    public boolean delPatientById(int id) {
        try {
            int flag = patientMapper.delPatientById(id);
            if (flag != -1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据id删除用户/患者时发生异常！");
        }
        return false;
    }
    /**
      * @description 根据当前用户id获得就诊状态
      * @author GUOYN
      * @date 2022-09-14 16:39:48  * @param userId
      * @return {@link int}
      */
    @Override
    public int getRegStateByIDCard(int userId) {
        try {
            log.info("{}",patientMapper.getRegStateByIDCard(userId));
            if (patientMapper.getRegStateByIDCard(userId)!=null){
                return patientMapper.getRegStateByIDCard(userId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据当前用户id获得就诊状态时发生异常！");
        }
        return 0;
    }
    /**
      * @description 根据当前用户id获得就诊时间
      * @author GUOYN
      * @date 2022-09-14 16:40:25  * @param id
      * @return {@link Date}
      */
    @Override
    public List<Date> getRecordTimeByIDCard(int userId) {
        try {
            return patientMapper.getRecordTimeByIDCard(userId);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据当前用户id获得就诊时间时发生异常！");
        }
        return null;
    }

    @Override
     /**
      *@author WangLuDong
      *@Description 方法描述 修改用户患者个人信息
      *@Date 2022-09-16 09:48:02
       * @param patient
      *@Return {@link boolean}
      */
    public boolean modifyPatientById(Patient patient) {
        log.info("进入修改...");
        try {
            return patientMapper.modifyPatientById(patient) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改信息失败");
        }
        return false;
    }

    @Override
    public Patient getPatientAll(int id) {
        try{
            return patientMapper.getOne(id);
        }catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }
}
